function [PR,R,motivations,alpha]=partition_representation(D,cl)
%PARTITION_REPRESENTATION finds the approximation of the data D given by
%the clustering cl.  PR is the representation and alpha are the coordinates
%(with respect to the clusters) of each node.  In this code, we do not
%normalize the data.  For some applications (for example, the equities
%data), normalization may be appropriate.

for i=1:max(cl)
        cli{i}=find(cl==i);
        motivations(:,i)=nanmean(D(cli{i},:),1)';
        mot_normalized(:,i)=motivations(:,i)/sqrt(nansum(motivations(:,i).*motivations(:,i)));
        cln=mot_normalized;
        cln(isnan(cln)==1)=0; 
end
    for i=1:size(D,1)
        inds=find(isnan(D(i,:))==0);
        X=linsolve(cln(inds,:),D(i,inds)');
        alpha(i,:)=X';
    end
        
    PR=alpha*cln';
    R=D-PR;
    
    
    